package com.atguigu.gulimall.order.web;

import com.atguigu.gulimall.order.service.OrderService;
import com.atguigu.gulimall.order.vo.OrderConfirmVo;
import com.atguigu.gulimall.order.vo.OrderSubmitResultVo;
import com.atguigu.gulimall.order.vo.OrderSumbitVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.concurrent.ExecutionException;

@Controller
public class OrderWebController {

    @Autowired
    private OrderService orderService;

    @RequestMapping("toTrade")
    public String toTrade(Model model) throws ExecutionException, InterruptedException {
        OrderConfirmVo orderConfirmVo = orderService.OrderConfirmInfo();


        model.addAttribute("orderConfirmData",orderConfirmVo);
        return "confirm";
    }

    @PostMapping("/submitOrder")
    public String submitOrder(OrderSumbitVo sumbitVo,Model model){

        OrderSubmitResultVo resultVo = null;
        try {
            resultVo = orderService.submitOrder(sumbitVo);
        } catch (Exception e) {
            //TODO 这里异常信息可以放到RedirectAttributes的addFlashAttribute()里面,然后再前端展示
            return "redirect:http://order.gulimall.com/toTrade";
        }

        if (resultVo.getCode() == 0 ){
            model.addAttribute("orderSubmitResult",resultVo);
            return "pay";
        }else {
            //TODO 这里异常信息可以放到RedirectAttributes的addFlashAttribute()里面,然后再前端展示
            return "redirect:http://order.gulimall.com/toTrade";
        }
    }

}
